/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ddd.context;

import com.ddd.entity.Idtable;
import com.ddd.service.IDService;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Parameters {

    private Log logger = LogFactory.getLog(this.getClass());
    private final HttpServletRequest request;

    public Parameters(HttpServletRequest request) {
        this.request = request;
    }

    public String getUsername() {
        return this.getParameter("username");
    }

    public void setUsername(String val) {
        this.setParameter("username", val);
    }

    public int getCurrentPage() {
        String curr = this.getParameter("currentpage");
        //logger.debug("curr page = " + curr);
        if (curr == null) {
            this.setCurrentPage(1);
            return 1;
        } else {
            return Integer.parseInt(curr);
        }
    }

    public void setCurrentPage(int val) {
        this.setParameter("currentpage", String.valueOf(val));
    }

    public int getTotalPage() {
        return Integer.parseInt(this.getParameter("totalpage"));
    }

    public void setTotalSize(int totalSize) {
        this.setParameter("totalsize", String.valueOf(totalSize));

        if (totalSize % Context.PAGESIZE == 0) {
            this.setParameter("totalpage", String.valueOf(totalSize / Context.PAGESIZE));
        } else {
            this.setParameter("totalpage", String.valueOf((totalSize / Context.PAGESIZE) + 1));
        }

        //logger.debug("total size = " + this.getTotalPage());
    }

    public int getTotalSize() {
        return Integer.parseInt(this.getParameter("totalsize"));
    }

    public int getPageSize() {
        return Context.PAGESIZE;
    }

    public String getIP() {
        return request.getRemoteAddr();
    }

    public void grantBEAuthorize() {
        this.setParameter("be", "1");
    }

    public void grantFEAuthorize() {
        this.setParameter("fe", "1");
    }

    public boolean isBEAuthorized() {
        String authorized = this.getParameter("be");
        if (authorized == null) {
            return false;
        } else {
            return true;
        }
    }

    public boolean isFEAuthorized() {
        String authorized = this.getParameter("fe");
        if (authorized == null) {
            return false;
        } else {
            return true;
        }
    }

    public void doLogout() {
        if (request == null) {
            return;
        }

        HttpSession session = request.getSession(false);
        if (session == null) {
            return;
        }else{
            session.invalidate();
        }
        
        //session.setAttribute(Context.SESSION, null);
    }

    private String getParameter(String key) {
        if (request == null) {
            return null;
        }

        HttpSession session = request.getSession(false);
        if (session == null) {
            return null;
        }

        HashMap<String, String> parameters = (HashMap<String, String>) session.getAttribute(Context.SESSION);
        if (parameters == null){
            return null;
        }
        
        return parameters.get(key);
    }

    private void setParameter(String key, String val) {
        HttpSession session = request.getSession();

        HashMap<String, String> parameters = (HashMap<String, String>) session.getAttribute(Context.SESSION);
        if (parameters == null) {
            parameters = new HashMap<String, String>();
        }

        parameters.put(key, val);
        session.setAttribute(Context.SESSION, parameters);
    }

    public static void doSystemInit() {
        IDService service = new IDService();
        String[] tablenames = {"log", "negoitem", "news", "commision"};

        for (int i = 0; i < tablenames.length; i++) {
            try {
                service.load(tablenames[i]);
            } catch (Exception e) {
                Idtable idtable = new Idtable();
                idtable.setTablename(tablenames[i]);
                idtable.setCurrId(new Integer(0));
                service.save(idtable);
                System.out.println("created ID for : " + tablenames[i]);
            }
        }

    }
}
